//
//  FileManagerDemo.swift
//  firstProject
//
//  Created by 张彦辉 on 2017/5/3.
//  Copyright © 2017年 张彦辉. All rights reserved.
//

import UIKit
import SnapKit

class FileManagerDemo: UIViewController {
    let text = UITextField()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        text.borderStyle = UITextBorderStyle.line
        text.placeholder = "请输入路径"
        
        let button1 = UIButton()
        button1.backgroundColor = UIColor.red
        button1.setTitle("检查", for: .normal)
        button1.addTarget(self, action: #selector(button1Click), for: .touchUpInside)
        
        let button2 = UIButton()
        button2.backgroundColor = UIColor.blue
        button2.setTitle("创建文件", for: .normal)
        button2.addTarget(self, action: #selector(button2Click), for: .touchUpInside)
        
        let button3 = UIButton()
        button3.backgroundColor = UIColor.darkGray
        button3.setTitle("创建目录", for: .normal)
        button3.addTarget(self, action: #selector(button3Click), for: .touchUpInside)
        
        let button4 = UIButton()
        button4.backgroundColor = UIColor.gray
        button4.setTitle("删除", for: .normal)
        button4.addTarget(self, action: #selector(button4Click), for: .touchUpInside)
        
        self.view.addSubview(text)
        self.view.addSubview(button1)
        self.view.addSubview(button2)
        self.view.addSubview(button3)
        self.view.addSubview(button4)
        
        text.snp.makeConstraints { (maker) in
            maker.width.equalTo(300)
            maker.height.equalTo(40)
            maker.top.equalTo(100)
            maker.centerX.equalTo(self.view)
        }
        button1.snp.makeConstraints { (maker) in
            maker.top.equalTo(200)
            maker.left.equalTo(20)
        }
        button2.snp.makeConstraints { (maker) in
            maker.top.equalTo(button1)
            maker.left.equalTo(button1.snp.right).offset(5)
        }
        button3.snp.makeConstraints { (maker) in
            maker.top.equalTo(button2)
            maker.left.equalTo(button2.snp.right).offset(5)
        }
        button4.snp.makeConstraints { (maker) in
            maker.top.equalTo(button3)
            maker.left.equalTo(button3.snp.right).offset(5)
        }
        
        
        
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    


}
// MARK: - 按钮点击事件
extension FileManagerDemo{
    func button1Click() -> Void {
        if let path = text.text {
            let fals = FileUtil.exists(path: path)
            PublicUtil.alert(obj: self, title: "提示", msg: "文件是否存在：\(fals)")
        }
        
    }
    func button2Click() -> Void {
        if let path = text.text {
            let fals = FileUtil.createPath(filePath: path)
            PublicUtil.alert(obj: self, title: "提示", msg: "创建文件是否成功：\(fals)")
        }
    }
    
    func button3Click() -> Void {
        if let path = text.text {
            let fals = FileUtil.createPath(directoryPath: path)
            PublicUtil.alert(obj: self, title: "提示", msg: "创建目录是否成功：\(fals)")
        }
    }
    
    func button4Click() -> Void {
        if let path = text.text {
            let fals = FileUtil.remove(path: path)
            PublicUtil.alert(obj: self, title: "提示", msg: "删除目录 或 文件是否成功：\(fals)")
        }
    }
}
